#%%
from os import replace
import plotly.graph_objects as go
import numpy as np
import random
import pandas as pd
from numpy.lib.function_base import average
import statistics
import pikepdf
from pikepdf import Pdf
import json
import random
import re
import xlsxwriter as xl
#%%
data_prel = [
{
"question_number": 1,
"description": "£9.95 with 1% probability and £-0.10 otherwise.",
"repetitions": 1,
"prob_up": 0.01,
"pay_up": 9.95,
"pay_down": -0.1,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 2,
"description": "£7.00 with 2% probability and £-0.14 otherwise.",
"repetitions": 1,
"prob_up": 0.02,
"pay_up": 7,
"pay_down": -0.14,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 3,
"description": "£4.36 with 5% probability and £-0.23 otherwise.",
"repetitions": 1,
"prob_up": 0.05,
"pay_up": 4.36,
"pay_down": -0.23,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 4,
"description": "£3.00 with 10% probability and £-0.33 otherwise.",
"repetitions": 1,
"prob_up": 0.1,
"pay_up": 3,
"pay_down": -0.33,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 5,
"description": "£2.00 with 20% probability and £-0.50 otherwise.",
"repetitions": 1,
"prob_up": 0.2,
"pay_up": 2,
"pay_down": -0.5,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 6,
"description": "£1.00 with 50% probability and £-1.00 otherwise.",
"repetitions": 1,
"prob_up": 0.5,
"pay_up": 1,
"pay_down": -1,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 7,
"description": "£0.50 with 80% probability and £-2.00 otherwise.",
"repetitions": 1,
"prob_up": 0.8,
"pay_up": 0.5,
"pay_down": -2,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 8,
"description": "£0.33 with 90% probability and £-3.00 otherwise.",
"repetitions": 1,
"prob_up": 0.9,
"pay_up": 0.33,
"pay_down": -3,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 9,
"description": "£0.23 with 95% probability and £-4.36 otherwise.",
"repetitions": 1,
"prob_up": 0.95,
"pay_up": 0.23,
"pay_down": -4.36,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 10,
"description": "£0.14 with 98% probability and £-7.00 otherwise.",
"repetitions": 1,
"prob_up": 0.98,
"pay_up": 0.14,
"pay_down": -7,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 11,
"description": "£0.10 with 99% probability and £-9.95 otherwise.",
"repetitions": 1,
"prob_up": 0.99,
"pay_up": 0.1,
"pay_down": -9.95,
"line_bool": False,
"repetition_word": "(one)",
},
{
"question_number": 12,
"description": "£0.33 with 90% probability and £-3.00 otherwise; repeated up to five times.",
"repetitions": 5,
"prob_up": 0.9,
"pay_up": 0.33,
"pay_down": -3,
"line_bool": False,
"repetition_word": "(five)",
},
{
"question_number": 13,
"description": "£0.33 with 90% probability and £-3.00 otherwise; repeated up to ten times.",
"repetitions": 10,
"prob_up": 0.9,
"pay_up": 0.33,
"pay_down": -3,
"line_bool": False,
"repetition_word": "(ten)",
},
{
"question_number": 14,
"description": "£0.33 with 90% probability and £-3.00 otherwise; repeated up to 50 times.",
"repetitions": 50,
"prob_up": 0.9,
"pay_up": 0.33,
"pay_down": -3,
"line_bool": False,
"repetition_word": "(fifty)",
},
{
"question_number": 15,
"description": "£0.33 with 90% probability and £-3.00 otherwise; repeated up to 100 times.",
"repetitions": 100,
"prob_up": 0.9,
"pay_up": 0.33,
"pay_down": -3,
"line_bool": False,
"repetition_word": "(one hundred)",
},
]
data = [
[
[0.5, 0.5],
[3, -3],
"202003ComprehensionLottery",
"choices/202003ComprehensionLottery.png",
False,
1,
],
[
[0.5, 0.5],
[3, -3],
"202003ComprehensionLottery_line",
"choices/202003ComprehensionLottery_line.png",
True,
1,
],
[
[0.2, 0.8],
[6.0, -1.50],
"gamble_risk_over_money_noline",
"choices/gamble_risk_over_money_noline.png",
False,
1,
],
[
[0.2, 0.8],
[6.0, -1.50],
"gamble_risk_over_money",
"choices/gamble_risk_over_money.png",
True,
1,
],
]
data_repeated = [
[
[0.9, 0.1],
[0.33, -3.0],
"repeated_gamble",
"repeat_choices/repeated_gamble.png",
False,
2,
],
[
[0.9, 0.1],
[0.3, -2.7],
"explain_repeated_gamble",
"repeat_choices/explain_repeated_gamble.png",
False,
2,
],
]
#%%
df_verify_final = pd.read_excel(
r"C:\Users\bernh\Code\SkewPremExp\SkewPremExp\static_data\210527_BonnusFileList.xlsx",
"Sheet1",
)
df_gambles = pd.DataFrame(data_prel)
#%%
df_verify_final["endowment"] = pd.to_numeric(df_verify_final["Endowment"].str[1:])
df_verify_final["pay_up"] = pd.to_numeric(
df_verify_final["Option 1"].apply(
lambda x: re.search("(£)(\d*[.,]?\d*)( )", x).group(2)
)
)
df_verify_final["pay_down"] = pd.to_numeric(
df_verify_final["Option 1"].apply(
lambda x: re.search("(£)(-\d*[.,]?\d*)( )", x).group(2)
)
)
df_verify_final["prob_up"] = (
pd.to_numeric(
df_verify_final["Option 1"].apply(
lambda x: re.search("(with )(\d*)(%)", x).group(2)
)
)
/ 100
)
df_verify_final["prob_down"] = 1 - df_verify_final["prob_up"]
df_verify_final["repetitions"] = df_verify_final["Option 1"].apply(
lambda x: re.search("(; repeated up to )(\w*)( times)", x).group(2)
if re.search("(; repeated up to )(\w*)( times)", x) != None
else 1
)
df_verify_final["repetitions"].replace("ten", 10, inplace=True)
df_verify_final["repetitions"].replace("five", 5, inplace=True)
df_verify_final["repetitions"] = pd.to_numeric(df_verify_final["repetitions"])
df_verify_final["option_2"] = pd.to_numeric(
df_verify_final["Option 2"].apply(
lambda x: re.search("(£)([-]?\d*[.,]?\d*)", x).group(2)
)
)
df_verify_final["pictures"] = df_verify_final["Bonus file"].apply(
lambda x: f"choices/choice_no_{x}.png"
)
#%%
df_test = pd.merge(
left=df_verify_final,
right=df_gambles,
left_on="Option 1",
right_on="description",
how="left",
)
print(df_test["pay_up_x"].equals(df_test["pay_up_y"]))
print(df_test["pay_down_x"].equals(df_test["pay_down_y"]))
print(df_test["prob_up_x"].equals(df_test["prob_up_y"]))
print(df_test["repetitions_x"].equals(df_test["repetitions_y"]))
df_test.head(5)
#%%
otree_data = [
[
row["Bonus file"],
row["question_number"],
row["endowment"],
row["Option 1"].replace("£", "_p_"),
row["option_2"],
[row["pay_up_x"], row["pay_down_x"]],
[row["prob_up_x"], row["prob_down"]],
row["pictures"],
row["repetitions_x"],
row["repetition_word"]
]
for i, row in df_test.iterrows()
]
with open("otree_data.json", "w") as f:
json.dump(otree_data, f)
#%% Seldomly used
def fixed_amount_panel(
amount, line_bool,
):
fig = go.Figure()
fig.layout.font.family = "Arial"
x_left = -0.2
x_cent = 1.2
x_right = 2.6
y_top = 1.7
y_mid = 1.5
y_bot = -0.2
fig.update_xaxes(
showgrid=False, zeroline=False, visible=False, range=[x_left, x_cent],
)
fig.update_yaxes(
showgrid=False,
zeroline=False,
visible=False,
range=[y_bot, y_mid],
scaleanchor="x",
scaleratio=1,
)
fig.update_layout(
showlegend=False,
margin=dict(l=0, r=0, t=0, b=0),
paper_bgcolor="rgba(255,255,255,1)",
plot_bgcolor="rgba(255,255,255,1)",
width=(904 * 0.75),
height=((1095 * 0.75 + 200) * (y_mid - y_bot) / (1.7 + 0.2 - -0.2)),
font=dict(
# family="Courier New, monospace",
size=26,
color="rgba(0,0,0,1)",
),
)
fig.add_trace(
go.Scatter(
x=[
x_left,
x_cent,
x_cent,
# x_cent,
# x_right,
# x_right,
# x_cent,
x_left,
x_left,
],
y=[
y_bot,
y_bot,
y_mid,
# y_bot,
# y_bot,
# y_mid,
# y_mid,
y_mid,
y_bot
#
],
mode="lines",
marker=dict(
color="rgba(0,0,0,0)" if line_bool == False else "rgba(0,0,0,1)"
),
)
)
fig.add_annotation(
x=statistics.mean([x_left, x_cent]),
xanchor="center",
y=statistics.mean([y_bot, y_mid]),
yanchor="middle",
text="£{:.2f}".format(amount),
showarrow=False,
font=dict(size=80,),
)
# fig.add_annotation(
# x=0.55, xanchor="left", y=0.45, text=f"If red", showarrow=False,
# )
return fig
fig = fixed_amount_panel(0.50, True)
fig.write_image(
f"C:/Users/bernh/Code/SkewPremExp/SkewPremExp/_static/choices/Screen3_fixed_amount.png"
)
print(f"Completed risk: Screen3_fixed_amount")
#%%
def urn_risk(
pay_up, pay_down, prob_up, line_bool, repetitions,
):
prob_up = int(prob_up * 100)
prob_down = int(100 - prob_up)
fig = go.Figure()
fig.layout.font.family = "Arial"
x_left = -0.2
x_cent = 1.2
x_right = 2.6
y_top = 1.9 if repetitions > 1 else 1.7
y_mid = 1.5
y_bot = -0.2
fig.update_xaxes(
showgrid=False, zeroline=False, visible=False, range=[x_left, x_cent],
)
fig.update_yaxes(
showgrid=False,
zeroline=False,
visible=False,
range=[y_bot, y_mid],
scaleanchor="x",
scaleratio=1,
)
fig.update_layout(
showlegend=False,
margin=dict(l=0, r=0, t=0, b=0),
paper_bgcolor="rgba(255,255,255,1)",
plot_bgcolor="rgba(255,255,255,1)",
width=(904 * 0.75),
height=((1095 * 0.75 + 200) * (y_mid - y_bot) / (1.7 + 0.2 - -0.2)),
font=dict(
# family="Courier New, monospace",
size=26,
color="rgba(0,0,0,1)",
),
)
fig.add_trace(
go.Scatter(
x=[
x_left,
x_cent,
x_cent,
# x_cent,
# x_right,
# x_right,
# x_cent,
x_left,
x_left,
],
y=[
y_bot,
y_bot,
y_mid,
# y_bot,
# y_bot,
# y_mid,
# y_mid,
y_mid,
y_bot
#
],
mode="lines",
marker=dict(
color="rgba(0,0,0,0)" if line_bool == False else "rgba(0,0,0,1)"
),
)
)
fig.add_trace(
go.Scatter(
x=[-0.1, -0.1, 0.5, 0.5],
y=[0.6, -0.1, -0.1, 0.6],
mode="lines",
marker=dict(color="rgba(0,0,0,1)"),
)
)
fig.add_trace(
go.Scatter(
x=[
elem
for i in range(10)
for elem in np.linspace(-0.1 + 0.05, 0.5 - 0.05, 10)
],
y=[
elem
for elem in np.linspace(-0.1 + 0.05, 0.6 - 0.05, 10)
for i in range(10)
],
mode="markers",
marker=dict(
size=15,
# color=random.sample(
# ["#1f77b4" for i in range(prob_down)]
# + ["#d62728" for i in range(prob_up)],
# 100,
# ),
color=["#1f77b4" for i in range(prob_down)]
+ ["#d62728" for i in range(prob_up)],
),
)
)
fig.add_trace(
go.Scatter(
x=[0.725, 0.74, 0.35, 0.35],
y=[0.45, 0.05, 0.7, 0.85],
mode="markers",
marker=dict(size=15, color=["#d62728", "#1f77b4", "#1f77b4", "#d62728"]),
)
)
if repetitions > 1:
fig.add_annotation(
x=statistics.mean([x_left, x_cent]),
xanchor="center",
y=1.3,
text="Single risk: With {}% probability
the outcome is £{:.2f} and with {}%
probability the outcome is £{:.2f}".format(
prob_up, pay_up, prob_down, pay_down
),
showarrow=False,
)
else:
fig.add_annotation(
x=statistics.mean([x_left, x_cent]),
xanchor="center",
y=1.3,
text="With {}% probability the
outcome is £{:.2f} and with {}%
probability the outcome is £{:.2f}".format(
prob_up, pay_up, prob_down, pay_down
),
showarrow=False,
)
fig.add_annotation(
x=0.55, xanchor="left", y=0.45, text=f"If red", showarrow=False,
)
fig.add_annotation(
x=0.55, xanchor="left", y=0.05, text=f"If blue", showarrow=False,
)
fig.add_annotation(
x=0.775,
xanchor="left",
y=0.45,
text="you get £{:.2f}".format(pay_up),
showarrow=False,
)
fig.add_annotation(
x=0.775,
xanchor="left",
y=0.05,
text="you get £{:.2f}".format(pay_down),
showarrow=False,
)
fig.add_annotation(
x=0.0, xanchor="left", y=0.85, text=f"{prob_up}/100: red", showarrow=False,
)
fig.add_annotation(
x=0.0, xanchor="left", y=0.7, text=f"{prob_down}/100: blue", showarrow=False,
)
return fig
# urn_risk(3, -3, 0.5, 0.5, 1)
#%%
for risk in data:
fig = urn_risk(risk[1][0], risk[1][1], risk[0][0], risk[4], risk[5])
fig.write_image(
f"C:/Users/bernh/Code/SkewPremExp/SkewPremExp/_static/choices/{risk[2]}.png"
)
print(f"Completed risk: {risk[2]}")
for risk in data_repeated:
fig = urn_risk(risk[1][0], risk[1][1], risk[0][0], risk[4], risk[5])
fig.write_image(
f"C:/Users/bernh/Code/SkewPremExp/SkewPremExp/_static/repeat_choices/{risk[2]}.png"
)
print(f"Completed risk: {risk[2]}")
for risk in data_prel:
fig = urn_risk(
risk["pay_up"],
risk["pay_down"],
risk["prob_up"],
risk["line_bool"],
risk["repetitions"],
)
fig.write_image(
f"C:/Users/bernh/Code/SkewPremExp/SkewPremExp/_static/choices/choice_no_{risk['question_number']}.png"
)
print(f"completed choice_number: {risk['question_number']}")
#%%
def gen_envelopes(
pay_up, pay_down, prob_up, o2_offer, endowment, repetitions, repetitions_word
):
prob_up = int(prob_up * 100)
prob_down = int(100 - prob_up)
fig = go.Figure()
fig.layout.font.family = "Arial"
x_left = -0.2
x_cent = 1.2
x_right = 2.6
y_top = 1.9 if repetitions > 1 else 1.7
y_mid = 1.5
y_bot = -0.2
fig.update_xaxes(
showgrid=False,
zeroline=False,
visible=False,
range=[x_left - 0.05, x_right + 0.05],
)
fig.update_yaxes(
showgrid=False,
zeroline=False,
visible=False,
range=[y_bot, y_top + 0.2],
scaleanchor="x",
scaleratio=1,
)
height_var = (1095 * 0.75 + 200) * (y_top + 0.2 - y_bot) / (1.7 + 0.2 - -0.2)
width_var = 904 * 0.75 * 2
# print(f"dimensions: {height_var}, {width_var}")
fig.update_layout(
showlegend=False,
margin=dict(l=0, r=0, t=0, b=0),
paper_bgcolor="rgba(255,255,255,1)",
plot_bgcolor="rgba(255,255,255,1)",
width=width_var,
height=height_var,
font=dict(
# family="Courier New, monospace",
size=26,
color="rgba(0,0,0,1)",
),
)
fig.add_trace(
go.Scatter(
x=[
x_left,
x_cent,
x_cent,
x_cent,
x_right,
x_right,
x_cent,
x_left,
x_left,
],
y=[y_bot, y_bot, y_mid, y_bot, y_bot, y_mid, y_mid, y_mid, y_bot],
mode="lines",
marker=dict(color="rgba(0,0,0,1)"),
)
)
fig.add_trace(
go.Scatter(
x=[
x_left,
x_cent,
x_cent,
x_cent,
x_right,
x_right,
x_cent,
x_left,
x_left,
],
y=[y_mid, y_mid, y_top, y_mid, y_mid, y_top, y_top, y_top, y_mid],
mode="lines",
marker=dict(color="rgba(0,0,0,1)"),
)
)
fig.add_trace(
go.Scatter(
x=[-0.1, -0.1, 0.5, 0.5],
y=[0.6, -0.1, -0.1, 0.6],
mode="lines",
marker=dict(color="rgba(0,0,0,1)"),
)
)
fig.add_trace(
go.Scatter(
x=[
elem
for i in range(10)
for elem in np.linspace(-0.1 + 0.05, 0.5 - 0.05, 10)
],
y=[
elem
for elem in np.linspace(-0.1 + 0.05, 0.6 - 0.05, 10)
for i in range(10)
],
mode="markers",
marker=dict(
size=15,
# color=random.sample(
# ["#1f77b4" for i in range(prob_down)]
# + ["#d62728" for i in range(prob_up)],
# 100,
# ),
color=["#1f77b4" for i in range(prob_down)]
+ ["#d62728" for i in range(prob_up)],
),
)
)
fig.add_trace(
go.Scatter(
x=[0.725, 0.74, 0.35, 0.35],
y=[0.45, 0.05, 0.7, 0.85],
mode="markers",
marker=dict(size=15, color=["#d62728", "#1f77b4", "#1f77b4", "#d62728"]),
)
)
if repetitions > 1:
fig.add_annotation(
x=statistics.mean([x_left, x_cent]),
xanchor="center",
y=1.3,
text="Single risk: With {}% probability
the outcome is £{:.2f} and with {}%
probability the outcome is £{:.2f}".format(
prob_up, pay_up, prob_down, pay_down
),
showarrow=False,
)
else:
fig.add_annotation(
x=statistics.mean([x_left, x_cent]),
xanchor="center",
y=1.3,
text="With {}% probability the
outcome is £{:.2f} and with {}%
probability the outcome is £{:.2f}".format(
prob_up, pay_up, prob_down, pay_down
),
showarrow=False,
)
fig.add_annotation(
x=0.55, xanchor="left", y=0.45, text=f"If red", showarrow=False,
)
fig.add_annotation(
x=0.55, xanchor="left", y=0.05, text=f"If blue", showarrow=False,
)
fig.add_annotation(
x=0.775,
xanchor="left",
y=0.45,
text="you get £{:.2f}".format(pay_up),
showarrow=False,
)
fig.add_annotation(
x=0.775,
xanchor="left",
y=0.05,
text="you get £{:.2f}".format(pay_down),
showarrow=False,
)
fig.add_annotation(
x=0.0, xanchor="left", y=0.85, text=f"{prob_up}/100: red", showarrow=False,
)
fig.add_annotation(
x=0.0, xanchor="left", y=0.7, text=f"{prob_down}/100: blue", showarrow=False,
)
if repetitions > 1:
fig.add_annotation(
x=statistics.mean([x_left, x_cent]),
xanchor="center",
y=statistics.mean([y_top, y_mid]),
yanchor="middle",
text=f"Option 1: Take the risk {repetitions} {repetitions_word} times
or until losing the first time,
whichever happens first (and add
the outcome to my endowment):",
showarrow=False,
)
else:
fig.add_annotation(
x=statistics.mean([x_left, x_cent]),
xanchor="center",
y=statistics.mean([y_top, y_mid]),
yanchor="middle",
text=f"Option 1: Take risk
(i.e., adjust my endowment by the outcome of):",
showarrow=False,
)
# Option 1: Take the risk 4 times or until losing the first time, whichever happens first (and add the outcome to my endowment):
fig.add_annotation(
x=statistics.mean([x_right, x_cent]),
xanchor="center",
y=statistics.mean([y_top, y_mid]),
yanchor="middle",
text=f"Option 2: Take amount
(i.e., adjust my endowment by):",
showarrow=False,
)
fig.add_annotation(
x=statistics.mean([x_right, x_cent]),
xanchor="center",
y=statistics.mean([y_bot, y_mid]),
yanchor="middle",
text="£{:.2f}".format(o2_offer),
showarrow=False,
font=dict(size=80,),
)
fig.add_annotation(
x=statistics.mean([x_right, x_left]),
xanchor="center",
y=y_top + 0.1,
yanchor="middle",
text="Your endowment is £{:.0f}.".format(endowment),
showarrow=False,
)
return fig
def encrypt_pdf(password, filename):
pdf = Pdf.open(filename, allow_overwriting_input=True)
pdf.save(
filename, encryption=pikepdf.Encryption(owner=password, user=password, R=4)
)
# you can change the R from 4 to 6 for 256 aes encryption
pdf.close()
print(f"--> encrypted file: {filename}")
fig = gen_envelopes(3.00, -3.00, 0.9, 3.00, 5.00, 6, "(six)")
fig
# %%
for i, row in df_test.iterrows():
fig = gen_envelopes(
row["pay_up_x"],
row["pay_down_x"],
row["prob_up_x"],
row["option_2"],
row["endowment"],
row["repetitions_x"],
row["repetition_word"],
)
fig.write_image(
f"C:/Users/bernh/Code/SkewPremExp/SkewPremExp/_static/rnd_vars/BonusFile{row['Bonus file']}.pdf"
)
print(f"--> generated file BonusFile{row['Bonus file']}.pdf")
encrypt_pdf(
"Tiger50",
f"C:/Users/bernh/Code/SkewPremExp/SkewPremExp/_static/rnd_vars/BonusFile{row['Bonus file']}.pdf",
)
for i, row in df_test.iterrows():
fig = gen_envelopes(
row["pay_up_x"],
row["pay_down_x"],
row["prob_up_x"],
row["option_2"],
row["endowment"],
row["repetitions_x"],
row["repetition_word"],
)
fig.write_image(
f"C:/Users/bernh/Code/SkewPremExp/SkewPremExp/_static/rnd_vars/BonusFile{row['Bonus file']}_no_pwd.pdf"
)
print(f"--> generated file BonusFile{row['Bonus file']}.pdf")
fig = gen_envelopes(6.0, -1.5, 0.2, 0.5, 5, 1, "(one")
fig.write_image(
f"C:/Users/bernh/Code/SkewPremExp/SkewPremExp/_static/rnd_vars/BonusFile{999}.pdf"
)
print(f"--> generated file BonusFile{999}.pdf")
# encrypt_pdf(
# "pwd",
# f"C:/Users/bernh/Code/SkewPremExp/SkewPremExp/_static/rnd_vars/BonusFile{999}.pdf",
# )
#%%
def get_rnd_array(len):
rnd_list = list(random.sample(range(1, 101, 1), len))
rnd_list.sort()
return rnd_list
def generate_rnd_tables(path, data):
"""generate random numbers for all entries in a df and save them to excel and json to be used in the verification file and loaded during the experiment
Args:
path (System path): where to save the data with suffixes for excel and json
data (pd.DataFrame): a df containing at least the text description of the risk, the number of repetitions and the probability of winning a positive amount
"""
# write df to dict to be used in this function
verification_file_list = [
{
"description": decision["description"].replace("£", "_p_"),
"index": decision["question_number"],
"repetitions": decision["repetitions"],
"rnd_numbers": [
get_rnd_array(int(decision["prob_up"] * 100))
for i in range(decision["repetitions"])
],
}
for decision in data
]
for elem in verification_file_list:
if elem["index"] >= 12 and elem["index"] <= 14:
cur_len = len(elem["rnd_numbers"])
elem["rnd_numbers"] = verification_file_list[-1]["rnd_numbers"][:cur_len]
# verification_file_list = [
# {
# "description": row["Option 1_clear"],
# # "rnd_numbers": get_rnd_array(int(row["probs_x"][0] * 100)),
# "rnd_numbers": [
# get_rnd_array(int(row["probs_x"][0] * 100))
# for i in range(row["repetitions"])
# ],
# "index": i,
# "repetitions": row["repetitions"],
# }
# for i, row in data.iterrows()
# ]
# write to excel
wb = xl.Workbook(f"{path}_excel.xlsx")
ws = wb.add_worksheet()
ws.hide_gridlines()
row_init = 0
col_init = 0
for item in verification_file_list:
for sublist in item["rnd_numbers"]:
ws.write(row_init, col_init, item["index"] + 1)
ws.write(
row_init,
col_init + 1,
item["description"].replace("_p_", "£")
# + f" This risk is repeated {item['repetitions']} times.",
)
ws.write(
row_init,
col_init + 2,
"".join(
[
f"{no}, " if i < len(sublist) - 1 else f"{no}"
for i, no in enumerate(sublist)
]
),
)
row_init += 1
wb.close()
# dump to json
# print(verification_file_list)
with open(f"{path}_json.json", "w") as f:
json.dump(verification_file_list, f)
generate_rnd_tables("only_numbers_prel", data_prel)
#%%
############################################################################################
# Old stuff
# def risk_tree_figure(pay_up, pay_down, prob_up):
# prob_up = int(prob_up * 100)
# prob_down = int(100 - prob_up)
# fig = go.Figure()
# fig.layout.font.family = "Arial"
# fig.update_xaxes(showgrid=False, zeroline=False, visible=False, range=[-0.2, 1.2])
# fig.update_yaxes(
# showgrid=False,
# zeroline=False,
# visible=False,
# range=[-0.2, 1.5],
# scaleanchor="x",
# scaleratio=1,
# )
# fig.update_layout(
# showlegend=False,
# margin=dict(l=0, r=0, t=0, b=0),
# paper_bgcolor="rgba(255,255,255,0)",
# plot_bgcolor="rgba(255,255,255,0)",
# width=904 * 0.75,
# height=1095 * 0.75,
# font=dict(
# # family="Courier New, monospace",
# size=26,
# color="rgba(0,0,0,1)",
# ),
# )
# fig.add_trace(
# go.Scatter(
# x=[0.2 - 0.1, 0.8 - 0.1],
# y=[0.5, 0.8],
# mode="lines",
# marker=dict(color="rgba(0,0,0,1)"),
# )
# )
# fig.add_trace(
# go.Scatter(
# x=[0.2 - 0.1, 0.8 - 0.1],
# y=[0.5, 0.2],
# mode="lines",
# marker=dict(color="rgba(0,0,0,1)"),
# )
# )
# fig.add_trace(
# go.Scatter(
# x=[-0.2, -0.2, 1.2, 1.2, -0.2],
# y=[-0.2, 1.5, 1.5, -0.2, -0.2],
# mode="lines",
# marker=dict(color="rgba(0,0,0,1)"),
# )
# )
# fig.add_annotation(
# x=0.5,
# y=1.3,
# text=f"With {prob_up}% probability the
outcome is £{float(pay_up)} and with {prob_down}%
probability the outcome is £{float(pay_down)}",
# showarrow=False,
# )
# fig.add_annotation(
# x=0.5 - 0.1, y=0.7, text=f"{prob_up}%", showarrow=False, textangle=-30,
# )
# fig.add_annotation(
# x=0.5 - 0.1, y=0.3, text=f"{prob_down}%", showarrow=False, textangle=30,
# )
# fig.add_annotation(
# x=0.82 - 0.1, xanchor="left", y=0.8, text=f"£{float(pay_up)}", showarrow=False
# )
# fig.add_annotation(
# x=0.82 - 0.1,
# xanchor="left",
# y=0.2,
# text=f"£{float(pay_down)}",
# showarrow=False,
# )
# return fig
# fig = risk_tree_figure(data[0][1][0], data[0][1][1], data[0][0][0])
# fig.show()